package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.DesignBase;
import com.ruoyi.system.domain.DesignScheme;
import com.ruoyi.system.domain.DesignSchemeImage;
import com.ruoyi.system.dto.design.DesignBatchCreateDTO;
import com.ruoyi.system.service.DesignBaseService;
import com.ruoyi.system.service.DesignBatchService;
import com.ruoyi.system.service.DesignSchemeImageService;
import com.ruoyi.system.service.DesignSchemeService;
import com.ruoyi.system.utils.OperationLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 批量创建设计信息服务实现类
 */
@Service
public class DesignBatchServiceImpl implements DesignBatchService {

    @Autowired
    private DesignBaseService designBaseService;

    @Autowired
    private DesignSchemeService designSchemeService;

    @Autowired
    private DesignSchemeImageService designSchemeImageService;

    /**
     * 批量创建设计基础信息、设计方案及图片
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long batchCreate(DesignBatchCreateDTO batchCreateDTO) {
        // 获取当前用户信息
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();
        LocalDateTime now = LocalDateTime.now();

        // 1. 保存设计基础信息
        DesignBase designBase = batchCreateDTO.getDesignBase();
        // 设置基础信息的创建人
        designBase.setCreatorId(userId);
        designBase.setCreatorName(userName);
        designBase.setCreatedTime(now);
        designBase.setUpdatedTime(now);
        designBaseService.saveWithDesignNo(designBase);
        Long designBaseId = designBase.getId();

        // 记录设计基础信息创建日志
        OperationLogUtils.recordDesignStatusChange(
                designBaseId,
                designBase.getSchemeName(),
                null,
                designBase.getDesignStatus(),
                "批量创建设计基础信息"
        );
        // 2. 保存设计方案及图片
        List<DesignBatchCreateDTO.DesignSchemeWithImages> schemes = batchCreateDTO.getSchemes();
        for (DesignBatchCreateDTO.DesignSchemeWithImages schemeDTO : schemes) {
            DesignScheme scheme = new DesignScheme();
            // 复制方案基本信息
            scheme.setDesignBaseId(designBaseId);
            scheme.setSchemeName(schemeDTO.getSchemeName());
            scheme.setUseOnlineDesign(schemeDTO.getUseOnlineDesign());
            scheme.setOnlineDesignId(schemeDTO.getOnlineDesignId());
            scheme.setPanoramaUrl(schemeDTO.getPanoramaUrl());
            scheme.setSortOrder(schemeDTO.getSortOrder());
            scheme.setMainDrawingUrl(schemeDTO.getMainDrawingUrl());
            scheme.setDescription(schemeDTO.getDescription());
            scheme.setCreatorId(userId);
            scheme.setCreatorName(userName);
            scheme.setCreatedTime(now);
            scheme.setUpdatedTime(now);

            designSchemeService.save(scheme);
            Long schemeId = scheme.getId();

            // 记录方案创建日志
            OperationLogUtils.recordSchemeAdd(
                    schemeId,
                    scheme.getSchemeName(),
                    designBaseId,
                    "批量创建设计方案"
            );

            // 3. 保存方案图片
            List<DesignSchemeImage> images = schemeDTO.getImages();
            if (images != null && !images.isEmpty()) {
                for (DesignSchemeImage image : images) {
                    image.setSchemeId(schemeId);
                    image.setCreatedTime(now);
                    image.setSortOrder(image.getSortOrder() != null ? image.getSortOrder() : 0);

                    designSchemeImageService.save(image);

                    // 记录图片上传日志
                    String spaceName = image.getSpaceName();
                    OperationLogUtils.recordImageUpload(
                            image.getId(),
                            image.getFileName(),
                            schemeId,
                            spaceName,
                            "批量上传设计图片"
                    );
                }
            }
        }

        return designBaseId;
    }
}
